<?php

namespace Addons\Jinuser\Controller;
use Home\Controller\AddonsController;

//一些基础的放这，免得太长，主要分类操作的多


class BaseController extends AddonsController{
  function categorysave(){
		if(!IS_POST) $this->Error('未提交数据');
		$siteid = I('post.siteid');
		$columnid = I('post.columnid');
		$id = I('post.id');
		$lp = I('post.lp');
		//修改分类
		foreach($id as $key=>$val){
			$condition = array();
			$condition['id'] = $val;
			$condition['name'] = trim(I('post.name'.$val));
			$condition['title'] = trim(I('post.name'.$val));
			$condition['nosort'] = trim(I('post.nosort'.$val));
			M('category')->Save($condition);
			unset($condition);
		}
		//新增分类
		foreach($lp as $key=>$val){
			$condition = array();
			$condition['siteid'] = $siteid;
			$condition['columnid'] = $columnid;
			$condition['name'] = trim(I('post.nname'.$val));
			$condition['title'] = trim(I('post.nname'.$val));
			$condition['nosort'] = trim(I('post.nnosort'.$val));
			$condition['sort'] = 0;
			$condition['pid'] = I('post.npid'.$val);
			$condition['level'] = I('post.nlevel'.$val);
			$condition['parentpath'] = I('post.nparentpath'.$val);
			M('category')->Add($condition);
			unset($condition);
		}
		//分类排序
		$this->updateCateSort('category', $siteid, $columnid);
		S('category'.$siteid.$columnid, null);
		$this->success('新增或修改分类成功！', $_SERVER['HTTP_REFERER']);
	}
	
	function categorysort($siteid, $columnid){
		if(IS_POST){
			$idjson = I('post.idjson', '');
			$idjson = str_replace('&quot;','"',$idjson);
			if(get_magic_quotes_gpc()){
				$idjson = stripslashes($idjson);
			}
			$this->saveSeqList('Category',$idjson);
			S('category'.$siteid.$columnid, null);
			$this->Success('恭喜！保存分类排序分组信息成功。',$_SERVER['HTTP_REFERER']);
		}else{
			$columntitle = $this->getsitecolumntitle($siteid, $columnid);
			$funcid = $this->getsitecolumnfuncid($siteid, $columnid);
			$funcpath = $this->getfuncpath($funcid);
			$this->assign('listcontent', $this->seqList('Category', $siteid, $columnid));
			$this->assign('columntitle', $columntitle);
			$this->assign('columnid', $columnid);
			$this->assign('funcpath', $funcpath);
			$this->display();
		}
	}
	
	function categorydel($siteid, $columnid, $id){
		//删除分类
		$condition = "id=$id or parentpath like ',%$id%,' or parentpath like ',%$id' ";
        
		M('Category')->Where($condition)->Delete();
		S('category'.$siteid.$columnid, null);
		$this->success('恭喜删除分类成功！', $_SERVER['HTTP_REFERER']);
	}
	
	
	/*
	* 函数名：saveSeqList
	* 参数： $table 表格
	*		$jsondata 传递的数据 是json格式的
	* 说明：结合seqList函数 调整分类顺序和分组的数据 进行分类保存
	*/
	function saveSeqList($table,$jsondata){
		$arrSql = array();
		static $sortnum = 0; //sort排序
		$arrData = json_decode($jsondata,true);
		//调用递归函数
		$this->saveChildSeqList($arrData,$arrSql);
		$category = M($table);
		$category->startTrans();
		//对返回的array进行批量数据库操作
		foreach($arrSql as $key=>$val){
			$category->Save($val);
		}
		$category->commit();
	}
	
	public function saveChildSeqList($children,&$arrSql,$pid=0,$level=1,$parentpath='0'){
		static $sort = 0;
		$nosort = 0;
		foreach($children as $key=>$val){
			//print_r($val);
			$sort++;$nosort++;
			$arrSql[] = array('id'=>$val['id'],
							  'pid'=>$pid,
							  'sort'=>$sort,
							  'nosort'=>$nosort,
							  'childnum'=>is_array($val['children'])?1:0,
							  'level'=>$level,
							  'parentpath'=>$parentpath);
			if(is_array($val['children'])){
				//进行自身递归
				$this->saveChildSeqList($val['children'],
									   $arrSql,
									   $val['id'],
									   $level+1,
									   $parentpath.','.$val['id']);
			}
		}
	}	
	/*
	* 函数名：updateCateSort
	* 参数介绍：$table 表格名称
	* 说明：无限级分类 按排序号 对分类的顺序进行保存
	*/
	private function updateCateSort($table, $siteid, $columnid){
		$condition = array();
		$condition['siteid'] = $siteid;
		$condition['columnid'] = $columnid;
		$category = M($table);
		$list = $category->Where($condition)
						 ->Field('id,pid')
						 ->Order('nosort asc,id asc')
						 ->Select();
		$parentcate = array(); //存储一级分类
		$childcate = array();//存储除一级分类以外的分类
		foreach($list as $rs){
		  if($rs['pid']==0){
			  $parentcate[] = $rs;
		  }else{
			  $childcate[] = $rs;
		  }
		}
		unset($condition);
		$this->updateSort($condition, $childcate, $parentcate);
		$category->startTrans();
		foreach($condition as $key=>$val){
			$category->Save($val);
		}
		$category->commit();
		//print_r($condition);
	}
	//递归排序
	private function updateSort(&$condition,$childcate, $parentcate){
		static $sort = 0;
		foreach($parentcate as $rs){
		  $sort++;
		  $nextcate = array();
		  $othercate = array();
		  foreach($childcate as $rs1){
			if($rs1['pid']==$rs['id']){
				$nextcate[] = $rs1;
			}else{
				$othercate[] = $rs1;
			}
		  }
		  $child = 0;
		  if(count($nextcate)) $child = 1;
		  $condition[] = array('id'=>$rs['id'],'sort'=>$sort,'childnum'=>$child);
		  if($child == 1) $this->updateSort($condition, $othercate, $nextcate);
		}	
	}
        
      

        /*获取分类*/
	 function getcategory($siteid, $columnid){
		if(S('category'.$siteid.$columnid)){
			return S('category'.$siteid.$columnid);
		}else{
			$list = M('category')->Where('siteid='.$siteid.' and columnid='.$columnid)->Order('sort')->Select();
			S('category'.$siteid.$columnid, $list, 36000);
			return $list;
		}
	}
	
	/*显示分类*/
	public function showcategory($siteid, $columnid, $id=0){
		$html = '';
		$list = $this->getcategory($siteid, $columnid);
		foreach($list as $key=>$val){
			$html .= '<option value="'.$val['id'].'"';
			if($val['id']==$id) $html .= ' selected="selected"';
			$html .= '>';
			if($val['level']>=2) $html .= '┊┄';
			for($i=3; $i <= $val['level']; $i++) $html .= '┄┄';
			$html .= ' '.$val['name'].'</option>';
		}
		return $html;
	}
        
        public function savesort(){
		if(IS_POST){
			$siteid = I('post.siteid', 0);
			$id = I('post.id');
			if(is_array($id)){
				$sitemap = M('category');
				$sitemap->startTrans(); 
				foreach($id as $item){
					$data['id'] = $item;
					$data['nosort'] = I('post.nosort'.$item, 0);
					$sitemap->Save($data);
				}
				$sitemap->commit();
				$this->Success('恭喜：保存排序成功！', $_SERVER['HTTP_REFERER']);
			}else{
				$this->Error('错误：没有内容可以排序！');
			}
		}else{
			$this->Error('错误：未提交数据！');
		}
	}
        
       
}
?>
